#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    slotOpenSerialPort();

    setButtonIcon(ui->pushButton, ":/img/吊灯-关.png");
    setButtonIcon(ui->pushButton_2,":/img/百叶窗-叶子-关.png");
    setButtonIcon(ui->pushButton_3,":/img/风扇-关.png");
    setButtonIcon(ui->pushButton_4,":/img/空调-关.png");
}

void MainWindow::setButtonIcon(QPushButton *button, const QString &imagePath) {
   QPixmap pixmap(imagePath);
   if (pixmap.isNull()) {
       qDebug() << "图片加载失败:" << imagePath;
       return;
   }
   button->setIcon(QIcon(pixmap));
   button->setIconSize(pixmap.size());
}

void MainWindow::slotLoadSerialPort()
{
    QStringList m_serialPortName;
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts()) //获取有效的串口列表
    {
        m_serialPortName << info.portName();
        qWarning()<<"serialPortName:"<<info.portName();
    }
}

void MainWindow::slotCloseSerialPort()
{
    if (m_serialPort)
        m_serialPort->close();
}

void MainWindow::slotOpenSerialPort()
{
    m_serialPort = new QSerialPort(this);//新建一个窗口对象

    if(m_serialPort->isOpen())//判断对象是否已经打开，如果是就关掉
    {
        m_serialPort->clear();
        m_serialPort->close();
    }
    //设置串口名字
    m_serialPort->setPortName("COM1");
    //打开串口
    if (!m_serialPort->open(QIODevice::ReadWrite))//打开COM2串口，并且设置为读写状态
    {
        return;
    }

    m_serialPort->setBaudRate(9600);//波特率
    m_serialPort->setDataBits(QSerialPort::Data8);//数据位
    m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//数据流控制（暂时不管）

    m_serialPort->setParity(QSerialPort::NoParity);//奇偶校验位
    m_serialPort->setStopBits(QSerialPort::OneStop);//停止位


    //把串口的可读信号和槽函数连接
    connect(m_serialPort,SIGNAL(readyRead()),this,SLOT(slotReadData()));

    connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::slotSend);

}

void MainWindow::slotReadData()
{
//     QString data = m_serialPort->readAll();
//     qWarning()<<"接收数据："<<data;


    QByteArray data = m_serialPort->readAll();  // 读取串口数据
       QString decodedData = QString::fromLocal8Bit(data);   // 使用UTF-8解码
       qWarning() << "接收数据：" << decodedData;  // 输出解码后的字符串

       if (decodedData.contains("打开吊灯"))
       {
           QPixmap openPixmap(":/img/吊灯-开.png"); // 开启状态的图片
           ui->pushButton->setIcon(QIcon(openPixmap));
       }
       else if (decodedData.contains("关闭吊灯"))
       {
           QPixmap closePixmap(":/img/吊灯-关.png"); // 关闭状态的图片
           ui->pushButton->setIcon(QIcon(closePixmap));
       }
       else if (decodedData.contains("打开风扇"))
       {
           QPixmap openPixmap_3(":/img/风扇-开.png"); // 关闭状态的图片
           ui->pushButton_3->setIcon(QIcon(openPixmap_3));
       }
       else if (decodedData.contains("关闭风扇"))
       {
           QPixmap closePixmap_3(":/img/风扇-关.png"); // 关闭状态的图片
           ui->pushButton_3->setIcon(QIcon(closePixmap_3));
       }
       else if (decodedData.contains("打开百叶窗"))
       {
           QPixmap openPixmap_2(":/img/百叶窗-叶子-开.png"); // 关闭状态的图片
           ui->pushButton_2->setIcon(QIcon(openPixmap_2));
       }
       else if (decodedData.contains("关闭百叶窗"))
       {
           QPixmap closePixmap_2(":/img/百叶窗-叶子-关.png"); // 关闭状态的图片
           ui->pushButton_2->setIcon(QIcon(closePixmap_2));
       }
       else if (decodedData.contains("打开空调"))
       {
           QPixmap openPixmap_4(":/img/空调-开.png"); // 关闭状态的图片
           ui->pushButton_4->setIcon(QIcon(openPixmap_4));
       }
       else if (decodedData.contains("关闭空调"))
       {
           QPixmap closePixmap_4(":/img/空调-关.png"); // 关闭状态的图片
           ui->pushButton_4->setIcon(QIcon(closePixmap_4));
       }
}

void MainWindow::slotSend()
{
    if (m_serialPort)
    {
        if (!m_serialPort->isOpen()) {
            qWarning() << "请打开一个串口";
            return;
        }

        QString info = "hello";
        QByteArray sendBuf;
        //把字符串转为字节
        sendBuf.append(info);// = info.toLocal8Bit();
        m_serialPort->write(sendBuf);
    }
}




MainWindow::~MainWindow()
{
   delete ui;
}

void MainWindow::on_pushButton_clicked()
{
   slotSend();
}
